#include <iostream>
#include <unistd.h>
#include <thread>
#include <atomic>

void set(std::atomic<int> &num)
{
	while (true)
	{
		num = rand() % 100;
		std::cout << "set设置原子变量为:" << num << std::endl;
		sleep(1);
	}
}

void print(std::atomic<int> &num)
{
	while (num != 0)
	{
		std::cout << "原子变量为:" << num << std::endl;
		num = rand() % 100;
		std::cout << "print重新设置原子变量为:" << num << std::endl;
		sleep(1);
	}
}

int main()
{
	srand(static_cast<unsigned int>(time(0) ^ rand()));
	std::atomic<int> num(rand() % 100);

	std::thread t1(set, std::ref(num));
	std::thread t2(print, std::ref(num));
	t1.join();
	t2.join();
	return 0;
}